﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF;
using System.Web.Security;
using System.Data;
using System.Web.UI.HtmlControls;


namespace geekmeet
{
    /// <summary>
    /// The class that controls a users wall
    /// </summary>
    public partial class Wall : System.Web.UI.Page
    {
        GeekMeetService wcf = new GeekMeetService();
        Guid loggedInUserGUID = (Guid)Membership.GetUser().ProviderUserKey;
        Guid targetUserID;
        ProfileComposite targetUserProfile = new ProfileComposite();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            GetTargetUserID();
            GetTargetUserProfile();
            ClearNotification();
            LoadWallPosts();
            DisplayPageHeader();
            EditSitemap();

            // If user wants to delete a wallpost
            if (Request.QueryString["action"] == ("RemoveWallpost"))
            {
                int id = int.Parse(Request.QueryString["ID"]);
                wcf.RemoveWallPost(id);
                LoadWallPosts();
            }
        }

        /// <summary>
        /// Displays the sitemap with correct username
        /// </summary>
        private void EditSitemap()
        {
            // Show sitemap in format username > page
            SiteMap.CurrentNode.ParentNode.ReadOnly = false;
            SiteMap.CurrentNode.ParentNode.Title = (string)targetUserProfile.Username;
            SiteMap.CurrentNode.ParentNode.Url = "Profile.aspx?user=" + targetUserID.ToString();
        }

        /// <summary>
        /// Displays the header with username
        /// </summary>
        private void DisplayPageHeader()
        {
            string viewUsername = wcf.GetUserName(targetUserID);

            //If I am looking at my own friendlist 
            if (loggedInUserGUID == targetUserID)
                lblHeaderUsername.Text = "Min gästbok";
            else
            {
                // Check if username ends with 'S', if not, append s to user name
                bool endsWithS = (viewUsername.Length - 1 == 's' ? true : false);
                lblHeaderUsername.Text = (endsWithS == false ? viewUsername + "s" : viewUsername);
                lblHeaderUsername.Text += " gästbok";
            }
        }

        private void GetTargetUserProfile()
        {
             targetUserProfile = wcf.GetProfile(targetUserID);
        }

        /// <summary>
        /// Gets the id of the user that is being viewed
        /// </summary>
        private void GetTargetUserID()
        {
            //if the target user is the same as viewer - user looking at his own profile
            string providedTargetUser = Request.QueryString["user"];
            if (providedTargetUser == null || providedTargetUser == loggedInUserGUID.ToString())
            {
                targetUserID = loggedInUserGUID;
            }

            else
            {
                try
                {
                    targetUserID = Guid.Parse(providedTargetUser);
                }
                catch
                {
                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        /// <summary>
        /// Removes notifications
        /// </summary>
        private void ClearNotification()
        {
            wcf.SetWallMessagesIsRead();
        }

        /// <summary>
        /// Loads the wallposts and displays them to the user
        /// </summary>
        private void LoadWallPosts()
        {
            List<WallComposite> wallposts = new List<WallComposite>();
            wallposts = wcf.GetWallPosts(targetUserID);
            wallRepeater.DataSource = wallposts;
            wallRepeater.DataBind();
        }

        /// <summary>
        /// Sends a new wallpost to user
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string message = txtMessageText.Text;
            wcf.CreateWallPost(loggedInUserGUID, targetUserID, message);
            LoadWallPosts();
            txtMessageText.Text = "";
        }
    }
}